package com.egrid.point.bury.config;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * IndexConfig.java
 * Created at 2019-12-27
 * Created by Royal Warmer
 * Copyright (C) 2018 SAIC VOLKSWAGEN, All rights reserved.
 */
@Configuration
@Slf4j
public class IndexConfig {

    private static final String URL = "http://localhost:";


    @Autowired
    private Environment environment;


    public void applicationReadyEvent() {
        log.info("监测当前操作系统为: {}", System.getProperties().getProperty("os.name"));
        if (System.getProperties().getProperty("os.name").toLowerCase().contains("windows")) {
            //获取配置文件中的server.port
            String port = environment.getProperty("server.port");
            //获取配置文件中的server.servlet.context-path
            String contextPath = environment.getProperty("server.servlet.context-path");
            log.info("应用已经准备就绪 ... 启动默认浏览器");
            String url = URL + port;
            if (!StringUtils.isEmpty(contextPath)) {
                url = url + contextPath + "/";
            }
            Runtime runtime = Runtime.getRuntime();
            try {
                log.info("请求地址为:{}", url);
                runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
            } catch (IOException e) {
                log.debug(e.getMessage(), e);
            }
        }
    }

}
